﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace BingMapsSample
{
    public class Queryable1<T> : IQueryable<T>
    {
        public Expression Expression { get; private set; }
        public IQueryProvider Provider { get; private set; }

        public Type ElementType
        {
            get { return typeof(T); }
        }

        public Queryable1(QueryProvider1 provider = null, Expression expression = null)
        {
            Expression = expression ?? Expression.Constant(this);
            Provider = provider ?? new QueryProvider1();
        }

        public IEnumerator<T> GetEnumerator()
        {
            return Provider.Execute<IEnumerable<T>>(Expression).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotSupportedException();
        }
    }

    public class QueryProvider1 : IQueryProvider
    {
        public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
        {
            return new Queryable1<TElement>(this, expression);
        }

        public IQueryable CreateQuery(Expression expression)
        {
            throw new NotSupportedException();
        }

        public TResult Execute<TResult>(Expression expression)
        {
            return (TResult)Enumerable.Range(1, 10);
        }

        public object Execute(Expression expression)
        {
            throw new NotSupportedException();
        }
    }
}
